package dispinfo;

import javax.microedition.lcdui.Image;

public class Lib
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	static public Image resizeImage(Image img, int newWidth, int newHeight, boolean useAlpha)
	{
		int width = img.getWidth();
		int width1 = width-1;
		int height = img.getHeight();
		int height1 = height-1;
		int[] oldImage = new int[width*height];
	
		img.getRGB(oldImage, 0, width, 0, 0, width, height);
		
		int[] newImage = new int[newWidth*newHeight];
	
		int r00=0, g00=0, b00=0, a00=0, r01=0, g01=0, b01=0, a01=0, r10=0, g10=0, b10=0, a10=0, r11=0, g11=0, b11=0, a11=0; 
		int oxPrev = -1, oyPrev = -1;
		int ox = 0;
		int xcnt = 0;
		for (int x = 0; x < newWidth; x++)
		{
			int oy = 0;
			int ycnt = 0;
			for (int y = 0; y < newHeight; y++)
			{
				if ((oxPrev != ox) || (oyPrev != oy))
				{
					int rgb = oldImage[ox+oy*width];
					r00 = rgb & 0xFF;
					g00 = (rgb >> 8) & 0xFF;
					b00 = (rgb >> 16) & 0xFF;
					a00 = (rgb >> 24) & 0xFF;
					if (oy < height1)
					{
						rgb = oldImage[ox+(oy+1)*width];
						r01 = rgb & 0xFF;
						g01 = (rgb >> 8) & 0xFF;
						b01 = (rgb >> 16) & 0xFF;
						a01 = (rgb >> 24) & 0xFF;
					}
					else { r01 = r00; g01 = g00; b01 = b00; a01 = a00; }
					if (ox < width1)
					{
						rgb = oldImage[ox+1+oy*width];
						r10 = rgb & 0xFF;
						g10 = (rgb >> 8) & 0xFF;
						b10 = (rgb >> 16) & 0xFF;
						a10 = (rgb >> 24) & 0xFF;
					}
					else { r10 = r00; g10 = g00; b10 = b00; a10 = a00; }
					if (oy < height1 && ox < width1)
					{
						rgb = oldImage[ox+1+(oy+1)*width];
						r11 = rgb & 0xFF;
						g11 = (rgb >> 8) & 0xFF;
						b11 = (rgb >> 16) & 0xFF;
						a11 = (rgb >> 24) & 0xFF;
					}
					else { r11 = r00; g11 = g00; b11 = b00; a11 = a00; }
					
					oxPrev = ox;
					oyPrev = oy;
				}
				
				int cf1 = (newHeight-ycnt);
				int r1 = r00*cf1/newHeight + r01*ycnt/newHeight;
				int g1 = g00*cf1/newHeight + g01*ycnt/newHeight;
				int b1 = b00*cf1/newHeight + b01*ycnt/newHeight;
				int a1 = a00*cf1/newHeight + a01*ycnt/newHeight;
				int r2 = r10*cf1/newHeight + r11*ycnt/newHeight;
				int g2 = g10*cf1/newHeight + g11*ycnt/newHeight;
				int b2 = b10*cf1/newHeight + b11*ycnt/newHeight;
				int a2 = a10*cf1/newHeight + a11*ycnt/newHeight;
				
				int cf2 = (newWidth-xcnt);
				int r = r1*cf2/newWidth + r2*xcnt/newWidth;
				int g = g1*cf2/newWidth + g2*xcnt/newWidth;
				int b = b1*cf2/newWidth + b2*xcnt/newWidth;
				int a = a1*cf2/newWidth + a2*xcnt/newWidth;
				
				if (r > 255) r = 255;
				if (r < 0) r = 0;
				if (g > 255) g = 255;
				if (g < 0) g = 0;
				if (b > 255) b = 255;
				if (b < 0) b = 0;
				if (a > 255) a = 255;
				if (a < 0) a = 0;
				
				if (!useAlpha) a = (a < 64) ? 0 : 255;
				
				newImage[x+y*newWidth] = r | (g << 8) | (b << 16) | (a << 24);
				
				ycnt += height;
				if (ycnt > newHeight) { oy++; ycnt -= newHeight; }
			}
			
			xcnt += width;
			if (xcnt >= newWidth) { ox++; xcnt -= newWidth; }
		}

		return Image.createRGBImage(newImage, newWidth, newHeight, true);
	}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	private static final String str_Win1251Chars = 
		"\u0402\u0403\u201A\u0453\u201E\u2026\u2020\u2021"+
		"\u20AC\u2030\u0409\u2039\u040A\u040C\u040B\u040F"+
		"\u0452\u2018\u2019\u201C\u201D\u2022\u2013\u2014"+
		"\u0020\u2122\u0459\u203A\u045A\u045C\u045B\u045F"+
		"\u00A0\u040E\u045E\u0408\u00A4\u0490\u00A6\u00A7"+
		"\u0401\u00A9\u0404\u00AB\u00AC\u00AD\u00AE\u0407"+
		"\u00B0\u00B1\u0406\u0456\u0491\u00B5\u00B6\u00B7"+
		"\u0451\u2116\u0454\u00BB\u0458\u0405\u0455\u0457"+
		"\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417"+
		"\u0418\u0419\u041A\u041B\u041C\u041D\u041E\u041F"+
		"\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427"+
		"\u0428\u0429\u042A\u042B\u042C\u042D\u042E\u042F"+
		"\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437"+
		"\u0438\u0439\u043A\u043B\u043C\u043D\u043E\u043F"+
		"\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447"+
		"\u0448\u0449\u044A\u044B\u044C\u044D\u044E\u044F";
	
	
	private static String str_Decode8bit(byte[] array, String condingSeq)
	{
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < array.length; i++)
		{
			int chr = 0xFF&(int)array[i];
			result.append((chr < 128) ? (char)chr : condingSeq.charAt(chr-128));
		}
		return result.toString();
	}
	
	static public final int CODING_WIN1251 = 1000;
	
	static public String bytesToString(byte[] data, int coding)
	{
		int len = data.length;
		
		switch (coding)
		{
		case CODING_WIN1251:
			return str_Decode8bit(data, str_Win1251Chars);
		}
		return null;
	}		
	
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
}
